package cn.iocoder.yudao.module.project.controller.admin.kismaterielexwarehousehistory;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.project.controller.admin.kismaterielexwarehousehistory.vo.*;
import cn.iocoder.yudao.module.project.dal.dataobject.kismaterielexwarehousehistory.KisMaterielExWarehouseHistoryDO;
import cn.iocoder.yudao.module.project.service.kismaterielexwarehousehistory.KisMaterielExWarehouseHistoryService;

@Tag(name = "管理后台 - 物料出库单历史数据")
@RestController
@RequestMapping("/project/kis-materiel-ex-warehouse-history")
@Validated
public class KisMaterielExWarehouseHistoryController {

    @Resource
    private KisMaterielExWarehouseHistoryService kisMaterielExWarehouseHistoryService;

    @PostMapping("/create")
    @Operation(summary = "创建物料出库单历史数据")
    @PreAuthorize("@ss.hasPermission('project:kis-materiel-ex-warehouse-history:create')")
    public CommonResult<String> createKisMaterielExWarehouseHistory(@Valid @RequestBody KisMaterielExWarehouseHistorySaveReqVO createReqVO) {
        return success(kisMaterielExWarehouseHistoryService.createKisMaterielExWarehouseHistory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新物料出库单历史数据")
    @PreAuthorize("@ss.hasPermission('project:kis-materiel-ex-warehouse-history:update')")
    public CommonResult<Boolean> updateKisMaterielExWarehouseHistory(@Valid @RequestBody KisMaterielExWarehouseHistorySaveReqVO updateReqVO) {
        kisMaterielExWarehouseHistoryService.updateKisMaterielExWarehouseHistory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除物料出库单历史数据")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('project:kis-materiel-ex-warehouse-history:delete')")
    public CommonResult<Boolean> deleteKisMaterielExWarehouseHistory(@RequestParam("id") String id) {
        kisMaterielExWarehouseHistoryService.deleteKisMaterielExWarehouseHistory(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得物料出库单历史数据")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('project:kis-materiel-ex-warehouse-history:query')")
    public CommonResult<KisMaterielExWarehouseHistoryRespVO> getKisMaterielExWarehouseHistory(@RequestParam("id") String id) {
        KisMaterielExWarehouseHistoryDO kisMaterielExWarehouseHistory = kisMaterielExWarehouseHistoryService.getKisMaterielExWarehouseHistory(id);
        return success(BeanUtils.toBean(kisMaterielExWarehouseHistory, KisMaterielExWarehouseHistoryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得物料出库单历史数据分页")
    @PreAuthorize("@ss.hasPermission('project:kis-materiel-ex-warehouse-history:query')")
    public CommonResult<PageResult<KisMaterielExWarehouseHistoryRespVO>> getKisMaterielExWarehouseHistoryPage(@Valid KisMaterielExWarehouseHistoryPageReqVO pageReqVO) {
        PageResult<KisMaterielExWarehouseHistoryDO> pageResult = kisMaterielExWarehouseHistoryService.getKisMaterielExWarehouseHistoryPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, KisMaterielExWarehouseHistoryRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出物料出库单历史数据 Excel")
    @PreAuthorize("@ss.hasPermission('project:kis-materiel-ex-warehouse-history:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportKisMaterielExWarehouseHistoryExcel(@Valid KisMaterielExWarehouseHistoryPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<KisMaterielExWarehouseHistoryDO> list = kisMaterielExWarehouseHistoryService.getKisMaterielExWarehouseHistoryPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "物料出库单历史数据.xls", "数据", KisMaterielExWarehouseHistoryRespVO.class,
                        BeanUtils.toBean(list, KisMaterielExWarehouseHistoryRespVO.class));
    }

}